package com.jsnkj.basic.activity

import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.ViewTreeObserver
import com.jsnkj.basic.LibraryConfig
import com.jsnkj.basic.R
import com.jsnkj.basic.adapter.InviteBannerAdapter
import com.jsnkj.basic.bean.InviteBannerBean
import com.jsnkj.basic.config.SpConfig
import com.jsnkj.basic.databinding.ActivityGoInviteBinding
import com.jsnkj.basic.mvvm.BaseMVVMActivity
import com.jsnkj.basic.mvvm.HttpViewModel
import com.jsnkj.basic.util.MediaUtil
import com.jsnkj.basic.util.QRCodeUtil
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions


class GoInviteActivity : BaseMVVMActivity<ActivityGoInviteBinding, HttpViewModel>(),
    View.OnClickListener {
    override val layoutId: Int
        get() = R.layout.activity_go_invite


    companion object {
        fun goToInvite(activity: Activity, shareLink: String) {
            val intent = Intent(activity, GoInviteActivity::class.java)
            intent.putExtra("link", shareLink)
            activity.startActivity(intent)
        }
    }


    private var shareLink = ""
    var bitmapQR: Bitmap? = null

    private val mBannerList: ArrayList<InviteBannerBean> = ArrayList()

    /**
     * banner的适配器
     */
    var bannerAdapter: InviteBannerAdapter? = null
    var bannerIndex = 0


    override fun initView(savedInstanceState: Bundle?) {
        mVDB.clickListener = this

        shareLink = intent.getStringExtra("link") ?: ""
        if (shareLink.isEmpty()) {
            com.jsnkj.basic.util.ToastUtils.show("推广失败")
            onBackPressed()
            return
        }

        bitmapQR = QRCodeUtil.createQRCodeBitmap(
            shareLink, 200, 200, "UTF-8", "H", "1", Color.BLACK, Color.WHITE
        )

        initBanner()
    }


    private fun initBanner() {
        mBannerList.add(InviteBannerBean(0, R.drawable.bg_go_invite1, bitmapQR,R.drawable.a9))
        mBannerList.add(InviteBannerBean(1, R.drawable.bg_go_invite2, bitmapQR,R.drawable.a10))
        mBannerList.add(InviteBannerBean(2, R.drawable.bg_go_invite3, bitmapQR,R.drawable.a11))

        bannerAdapter = InviteBannerAdapter(this@GoInviteActivity, mBannerList)

        mVDB.banner.apply {
            setAdapter(bannerAdapter)
            setIndicator(mVDB.reIndicator, false)
            isAutoLoop(false)

            setOnBannerListener { data, _ ->
                bannerIndex = (data as InviteBannerBean).id
                com.jsnkj.basic.util.LogUtils.e("baner滑动： bannerIndex == $bannerIndex")
            }
        }
        // 获取 Banner 加载的实际布局
        val viewTreeObserver: ViewTreeObserver = mVDB.banner.viewTreeObserver
        viewTreeObserver.addOnGlobalLayoutListener(object :
            ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {

            }
        })

    }

    override fun onClick(view: View) {

        when (view.id) {
            R.id.tv_share_wx -> {
                val bitmap = getBannerBitmap()
                if (null == bitmap) {
                    com.jsnkj.basic.util.ToastUtils.show("分享失败")
                    return
                }
                com.jsnkj.basic.wxapi.WechatHelper.getInstance(this@GoInviteActivity.application, LibraryConfig.WX_APP_ID).shareImage(bitmap!!,0)
            }

            R.id.tv_share_pyq -> {
                //分享朋友圈
                val bitmap = getBannerBitmap()
                if (null == bitmap) {
                    com.jsnkj.basic.util.ToastUtils.show("分享失败")
                    return
                }
                com.jsnkj.basic.wxapi.WechatHelper.getInstance(this@GoInviteActivity.application, LibraryConfig.WX_APP_ID).shareImage(bitmap!!,1)
            }

          R.id.tv_down -> {
                postPermissions(view)
            }

            R.id.tv_copy_link -> {
                com.jsnkj.basic.util.AppUtil.copyClipboard(this@GoInviteActivity, shareLink)
                com.jsnkj.basic.util.ToastUtils.show("复制成功")
            }

            R.id.tv_close -> {
                onBackPressed()
            }

        }
    }


    fun getBannerBitmap(): Bitmap? {
        // Banner 已经布局完成，可以获取其内容进行操作
        val bannerView: View = mVDB.banner.getChildAt(bannerIndex) // 假设 Banner 的内容是放在第一个子视图中
        return MediaUtil.getViewBitmap(bannerView)
    }


    private fun postPermissions(view: View) {
        XXPermissions.with(this) // 申请单个权限
            .permission(Permission.Group.STORAGE)// 申请多个权限
            .request(object : OnPermissionCallback {
                override fun onGranted(permissions: List<String>, all: Boolean) {
                    if (all) {
                        clickItemView(view)
                    }
                }

                override fun onDenied(permissions: List<String>, never: Boolean) {
                    if (never) {
                        com.jsnkj.basic.util.ToastUtils.show("被永久拒绝授权，请手动授予存储权限")
                        // 如果是被永久拒绝就跳转到应用权限系统设置页面
                        XXPermissions.startPermissionActivity(
                            this@GoInviteActivity, permissions
                        )
                    } else {
                        com.jsnkj.basic.util.ToastUtils.show("获取存储权限失败")
                    }
                }
            })
    }


    fun clickItemView(view: View) {
        when (view.id) {
            R.id.tv_down -> {
                var bitmap = getBannerBitmap()
                if (null == bitmap) {
                    com.jsnkj.basic.util.ToastUtils.show("保存失败")
                    return
                }
                MediaUtil.savePuzzleBitmap(
                    this, bitmap, SpConfig.IMG_SAVE_NAME
                ) { state, _ ->
                    if (state) {
                        bitmap.recycle()
                        com.jsnkj.basic.util.ToastUtils.show("已成功保存到相册")
                    } else {
                        com.jsnkj.basic.util.ToastUtils.show("保存失败，请重试")
                    }
                }
            }
        }
    }

}


























